package com.dynamic.mybatis.core.utils;

/*import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.Document;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;*/

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;

/**
 * @Description
 * @Author xs
 * @Date 2023/4/17 10:25
 */
public class XmlUtil {
    /**
     * XML格式化输出默认缩进量
     */
    public static final int INDENT_DEFAULT = 2;

    public static String format(String xml) {
        return format(xml, StandardCharsets.UTF_8.displayName(), Boolean.TRUE, Boolean.TRUE);
    }

    public static String format(String xml, String charset, boolean isPretty, boolean omitXmlDeclaration) {
        return format(xml, charset, isPretty ? INDENT_DEFAULT : 0, omitXmlDeclaration);
    }

    /**
     * @param xml
     * @param charset            编码
     * @param indent             缩进
     * @param omitXmlDeclaration 是否输出文件头
     * @return
     */
    public static String format(String xml, String charset, int indent, boolean omitXmlDeclaration) {
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            if (indent > 0) {
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
            }
            if (StringUtils.isNotBlank(charset)) {
                transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            }
            if (omitXmlDeclaration) {
                transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            }
            StreamResult xmlOutput = new StreamResult(new StringWriter());
            transformer.transform(new StreamSource(new StringReader(xml)), xmlOutput);
            return xmlOutput.getWriter().toString();
        } catch (Exception e) {
            throw new RuntimeException("Trans xml document to string error!", e);
        }
    }


    public static void main(String[] args) {
        String xml = "<select id=\"selectByIdTest\" name=\"查询测试\" parameterType=\"map\" resultType=\"map\" returnType=\"map\">" +
                "SELECT id,id AS id,user_account AS userAccount,name AS name,password AS password,status AS status,birthday AS birthday,work_begin_time AS workBeginTime,work_end_time AS workEndTime,is_deleted AS isDeleted,create_time AS createTime,create_by AS createBy,update_time AS updateTime,update_by AS updateBy FROM test WHERE id=#{id}" +
                "</select>";
        System.out.println(format(xml));
    }
}